package com.guomei.service.curse.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guomei.bean.curse.CourseVideo;
import com.guomei.bean.curse.CourseVideoDetail;
import com.guomei.mapper.curse.CourseVideoMapper;
import com.guomei.service.curse.CourseVideoService;
import com.guomei.utils.PageData;
import com.guomei.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程视频表Service实现类
 *
 * @author yourname
 * @date 2025-08-22
 */
@Service
@Slf4j
@Transactional
public class CourseVideoServiceImpl implements CourseVideoService {
    @Resource
    private CourseVideoMapper courseVideoMapper;

    @Override
    public CourseVideo getById(Long id) {
        return courseVideoMapper.selectById(id);
    }

    @Override
    public List<CourseVideo> getByCourseId(Long courseId) {
        return courseVideoMapper.selectByCourseId(courseId);
    }

    @Override
    public List<CourseVideo> getByChapterId(Long chapterId) {
        return courseVideoMapper.selectByChapterId(chapterId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(CourseVideo courseVideo) {
        // 设置默认值
        if (courseVideo.getSortOrder() == null) {
            courseVideo.setSortOrder(0);
        }
        if (courseVideo.getStatus() == null) {
            courseVideo.setStatus(0); // 默认为转码中
        }
        if (courseVideo.getIsFree() == null) {
            courseVideo.setIsFree(0); // 默认为不免费
        }
        return courseVideoMapper.insert(courseVideo) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(CourseVideo courseVideo) {
        return courseVideoMapper.update(courseVideo) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Long id) {
        return courseVideoMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByCourseId(Long courseId) {
        return courseVideoMapper.deleteByCourseId(courseId) > 0;
    }

    @Override
    public Result<?> listDetail(CourseVideo courseVideo) {
        PageHelper.startPage(courseVideo.getPageNum(), courseVideo.getPageSize());
        List<CourseVideoDetail> courseVideoDetail = courseVideoMapper.selectDetailByCourseId(courseVideo.getCourseId());
        // 封装分页结果到PageData
        PageData<CourseVideoDetail> pageData = new PageData<>();
        PageInfo<CourseVideoDetail> pageInfo = new PageInfo<>(courseVideoDetail);
        pageData.setTotal(pageInfo.getTotal());
        pageData.setData(pageInfo.getList());
        pageData.setPageNum(courseVideo.getPageNum());
        pageData.setPageSize(courseVideo.getPageSize());
        return Result.success(pageData);
    }

    @Override
    public Result<?> listDetailByIds(List<Long> ids) {
        List<CourseVideoDetail> courseVideoDetails = courseVideoMapper.selectDetailByIds(ids);
        PageData<CourseVideoDetail> pageData = new PageData<>();
        pageData.setData(courseVideoDetails);
        pageData.setTotal(courseVideoDetails.size());
        return Result.success(pageData);
    }
}
